/*
 * SelectScheduleTypeDialog.java
 *
 * Created on 6 de abril de 2009, 15:44
 */

package ubadbtools.recoveryLogAnalyzer.redo.gui.forms;

import java.awt.Frame;
import java.util.Set;
import java.util.LinkedHashSet;

import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle;

import ubadbtools.recoveryLogAnalyzer.redo.common.logRecords.EndCheckPointLogRecord;
import ubadbtools.recoveryLogAnalyzer.redo.common.logRecords.RecoveryLogRecord;
import ubadbtools.recoveryLogAnalyzer.redo.common.logRecords.StartCheckPointLogRecord;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EditLogRecordDialog extends javax.swing.JDialog {
    
	//[start] Atributos
	private RecoveryLogRecord logRecord = null;
	private Set<String> availableTransactions;
	private Set<String> activeTransactions;
	private JRadioButton rbAbort;
	private JRadioButton rbStartCheckpoint;
	private JRadioButton rbEndCheckpoint;
	private Set<String> items;
	private Frame parent;
	//[end]
	
	//[start] Constructor
    public EditLogRecordDialog(java.awt.Frame parent, boolean modal, Set<String> availableTransactions, Set<String> items, Set<String> activeTransactions, boolean needEndCheckPoint)
    {
        super(parent, modal);
        initComponents();
        
        this.availableTransactions 	= availableTransactions;
        this.activeTransactions 	= activeTransactions;
        this.items 					= items;
        this.parent 				= parent;
             
        //Start solo esta habilitado si y solo si hay transacciones no activas para abrir
        rbStart.setEnabled(availableTransactions.size() > 0);
        rbStartCheckpoint.setEnabled(true);
    	rbEndCheckpoint.setEnabled(needEndCheckPoint);
    	rbAbort.setEnabled(activeTransactions.size()>0);
    	rbUpdate.setEnabled(activeTransactions.size()>0 && items.size()>0);
    	rbCommit.setEnabled(activeTransactions.size()>0);
    }
    //[end]
    
    //[start] showDialog
    public static RecoveryLogRecord showDialog(Frame parent, Set<String> availableTransactions, Set<String> items, Set<String> activeTransactions, boolean needEndCheckPoint)
    {
    	EditLogRecordDialog dialog = new EditLogRecordDialog(parent, true, availableTransactions, items, activeTransactions, needEndCheckPoint);
        dialog.setVisible(true);
        
        return dialog.logRecord;
    }
    //[end]
    
    //[start] InitComponents (AUTO-GENERATED)
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rbgActionType = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        rbStart = new javax.swing.JRadioButton();
        rbUpdate = new javax.swing.JRadioButton();
        rbCommit = new javax.swing.JRadioButton();
        rbStartCheckpoint = new javax.swing.JRadioButton();
        rbEndCheckpoint = new javax.swing.JRadioButton();
        rbCommit = new javax.swing.JRadioButton();
        butCancelar = new javax.swing.JButton();
        GroupLayout layout = new GroupLayout((JComponent)getContentPane());
        getContentPane().setLayout(layout);
        butAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        rbgActionType.add(rbStart);
        rbStart.setSelected(true);
        rbStart.setText("Start"); // NOI18N
        rbStart.setName("rbStart"); // NOI18N

        rbgActionType.add(rbUpdate);
        rbUpdate.setText("Update"); // NOI18N
        rbUpdate.setName("rbUpdate"); // NOI18N
        {
        	rbAbort = new JRadioButton();
        	rbAbort.setText("Abort");
        	rbAbort.setName("rbAbort");
        	rbgActionType.add(rbAbort);
        }

        rbgActionType.add(rbCommit);
        rbCommit.setText("Commit"); // NOI18N
        rbCommit.setName("rbCommit"); // NOI18N
        
        rbgActionType.add(rbStartCheckpoint);
        rbStartCheckpoint.setText("Start checkpoint"); // NOI18N
        rbStartCheckpoint.setName("rbStartCheckpoint"); // NOI18N
        
        rbgActionType.add(rbEndCheckpoint);
        rbEndCheckpoint.setText("End checkpoint"); // NOI18N
        rbEndCheckpoint.setName("rbEndCheckpoint"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
        	.addComponent(rbStart, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(rbUpdate, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(rbCommit, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(rbStartCheckpoint, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(rbEndCheckpoint, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(rbAbort, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE));
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
        	.addContainerGap()
        	.addGroup(jPanel1Layout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
        	        .addComponent(rbStart, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 82, Short.MAX_VALUE))
        	    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
        	        .addComponent(rbUpdate, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 68, Short.MAX_VALUE))
        	    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
        	        .addComponent(rbCommit, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 62, Short.MAX_VALUE))
        	    .addGroup(jPanel1Layout.createSequentialGroup()
        	        .addComponent(rbStartCheckpoint, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 0, Short.MAX_VALUE))
        	    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
        	        .addComponent(rbEndCheckpoint, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 10, Short.MAX_VALUE))
        	    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
        	        .addComponent(rbAbort, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 62, Short.MAX_VALUE)))
        	.addContainerGap(62, 62));

        butCancelar.setText("Cancelar"); // NOI18N
        butCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butCancelarMouseClicked(evt);
            }
        });

        butAceptar.setText("Aceptar"); // NOI18N
        butAceptar.setName("jButton1"); // NOI18N
        layout.setVerticalGroup(layout.createSequentialGroup()
        	.addContainerGap()
        	.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	    .addComponent(butAceptar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(butCancelar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addContainerGap(15, 15));
        layout.setHorizontalGroup(layout.createSequentialGroup()
        	.addContainerGap(177, 177)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addComponent(butAceptar, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addComponent(butCancelar, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 89, Short.MAX_VALUE))
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addPreferredGap(butAceptar, jPanel1, LayoutStyle.ComponentPlacement.INDENT)
        	        .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 0, Short.MAX_VALUE)))
        	.addContainerGap(97, 97));
        butAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butAceptarMouseClicked(evt);
            }
        });

        pack();
        this.setSize(499, 242);
    }// </editor-fold>//GEN-END:initComponents
	//[end]
    
    //[start] Eventos
    private void butAceptarMouseClicked(java.awt.event.MouseEvent evt)
    {//GEN-FIRST:event_butAceptarMouseClicked

    	// Genero el registro de log segun los valores en los controles del dialogo
    	if(rbStart.isSelected() && rbStart.isEnabled())
    		logRecord = StartLogRecordDialog.showDialog(parent, availableTransactions);
    	else if(rbCommit.isSelected() && rbCommit.isEnabled())
    		logRecord = CommitLogRecordDialog.showDialog(parent, activeTransactions);
    	else if(rbUpdate.isSelected() && rbUpdate.isEnabled())
    		logRecord = UpdateLogRecordDialog.showDialog(parent, activeTransactions,items);
    	else if(rbAbort.isSelected() && rbAbort.isEnabled())
    		logRecord = AbortLogRecordDialog.showDialog(parent, activeTransactions);
    	else if(rbEndCheckpoint.isSelected() && rbEndCheckpoint.isEnabled())
    		logRecord = new EndCheckPointLogRecord();
    	else if(rbStartCheckpoint.isSelected() && rbStartCheckpoint.isEnabled())
    	{
    		logRecord = new StartCheckPointLogRecord(activeTransactions);
    	}
    	
    	this.setVisible(false);
    }//GEN-LAST:event_butAceptarMouseClicked

    private void butCancelarMouseClicked(java.awt.event.MouseEvent evt)
    {//GEN-FIRST:event_butCancelarMouseClicked
        this.setVisible(false);
    }//GEN-LAST:event_butCancelarMouseClicked
    //[end]

    //[start] Variables Form (AUTO-GENERATED)
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butAceptar;
    private javax.swing.JButton butCancelar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton rbCommit;
    private javax.swing.JRadioButton rbStart;
    private javax.swing.JRadioButton rbUpdate;
    private javax.swing.ButtonGroup rbgActionType;
    // End of variables declaration//GEN-END:variables
    //[end]
}
